/*
 * // +-------------------------------------------------------------------------------------------------
 * // |                 有你就好 [ 有节骨乃坚，无心品自端 ]     <http://encoding.wang>
 * // +-------------------------------------------------------------------------------------------------
 * // |                             独在异乡为异客         每逢佳节倍思亲
 * // +-------------------------------------------------------------------------------------------------
 * // |                 联系:   <707069100@qq.com>      <http://weibo.com/513778937>
 * // +-------------------------------------------------------------------------------------------------
 */

// -----------------------------------------------------------------------------------------------------
// +----------------------------------------------------------------------------------------------------
// |                   ErYang出品 属于小极品          共同学习    共同进步
// +----------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------


package wang.encoding.mroot.plugin.ueditor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 路径格式化
 *
 * @author ErYang
 */
public class PathFormat {

    private static final String TIME = "time";
    private static final String FULL_YEAR = "yyyy";
    private static final String YEAR = "yy";
    private static final String MONTH = "mm";
    private static final String DAY = "dd";
    private static final String HOUR = "hh";
    private static final String MINUTE = "ii";
    private static final String SECOND = "ss";
    private static final String RAND = "rand";

    private static Date currentDate = null;

    private static final Pattern PATTERN = Pattern.compile("\\{([^\\}]+)\\}", Pattern.CASE_INSENSITIVE);

    /**
     * 解析
     *
     * @param input String
     * @return String
     */
    public static String parse(final String input) {
        Matcher matcher = PATTERN.matcher(input);
        PathFormat.currentDate = new Date();
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, PathFormat.getString(matcher.group(1)));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    // -------------------------------------------------------------------------------------------------

    /**
     * 格式化路径, 把windows路径替换成标准路径
     *
     * @param input 待格式化的路径
     * @return 格式化后的路径
     */
    public static String format(final String input) {
        return input.replace("\\", "/");
    }

    // -------------------------------------------------------------------------------------------------

    /**
     * 解析
     *
     * @param input    String
     * @param filename String
     * @return String
     */
    public static String parse(final String input, String filename) {
        Matcher matcher = PATTERN.matcher(input);
        String matchStr;
        PathFormat.currentDate = new Date();
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matchStr = matcher.group(1);
            if (matchStr.contains("filename")) {
                filename = filename.replace("$", "\\$").replaceAll("[\\/:*?\"<>|]", "");
                matcher.appendReplacement(sb, filename);
            } else {
                matcher.appendReplacement(sb, PathFormat.getString(matchStr));
            }
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    // -------------------------------------------------------------------------------------------------

    /**
     * 得到名称
     *
     * @param pattern String
     * @return String
     */
    private static String getString(String pattern) {
        pattern = pattern.toLowerCase();
        // time 处理
        if (pattern.contains(PathFormat.TIME)) {
            return PathFormat.getTimestamp();
        } else if (pattern.contains(PathFormat.FULL_YEAR)) {
            return PathFormat.getFullYear();
        } else if (pattern.contains(PathFormat.YEAR)) {
            return PathFormat.getYear();
        } else if (pattern.contains(PathFormat.MONTH)) {
            return PathFormat.getMonth();
        } else if (pattern.contains(PathFormat.DAY)) {
            return PathFormat.getDay();
        } else if (pattern.contains(PathFormat.HOUR)) {
            return PathFormat.getHour();
        } else if (pattern.contains(PathFormat.MINUTE)) {
            return PathFormat.getMinute();
        } else if (pattern.contains(PathFormat.SECOND)) {
            return PathFormat.getSecond();
        } else if (pattern.contains(PathFormat.RAND)) {
            return PathFormat.getRandom(pattern);
        }
        return pattern;
    }

    // -------------------------------------------------------------------------------------------------

    private static String getTimestamp() {
        return System.currentTimeMillis() + "";
    }

    // -------------------------------------------------------------------------------------------------

    private static String getFullYear() {
        return new SimpleDateFormat("yyyy").format(PathFormat.currentDate);
    }

    // -------------------------------------------------------------------------------------------------

    private static String getYear() {
        return new SimpleDateFormat("yy").format(PathFormat.currentDate);
    }

    // -------------------------------------------------------------------------------------------------

    private static String getMonth() {
        return new SimpleDateFormat("MM").format(PathFormat.currentDate);
    }

    // -------------------------------------------------------------------------------------------------

    private static String getDay() {
        return new SimpleDateFormat("dd").format(PathFormat.currentDate);
    }

    // -------------------------------------------------------------------------------------------------

    private static String getHour() {
        return new SimpleDateFormat("HH").format(PathFormat.currentDate);
    }

    // -------------------------------------------------------------------------------------------------

    private static String getMinute() {
        return new SimpleDateFormat("mm").format(PathFormat.currentDate);
    }

    // -------------------------------------------------------------------------------------------------

    private static String getSecond() {
        return new SimpleDateFormat("ss").format(PathFormat.currentDate);
    }

    // -------------------------------------------------------------------------------------------------

    /**
     * 获得随机数
     *
     * @param pattern String
     * @return String
     */
    private static String getRandom(String pattern) {
        int length;
        pattern = pattern.split(":")[1].trim();
        length = Integer.parseInt(pattern);
        return (Math.random() + "").replace(".", "").substring(0, length);
    }

    // -------------------------------------------------------------------------------------------------

}

// -----------------------------------------------------------------------------------------------------

// End PathFormat class

/* End of file PathFormat.kt */
/* Location: ./src/main/kotlin/wang/encoding/mroot/plugin/ueditor/PathFormat.kt */

// -----------------------------------------------------------------------------------------------------
// +----------------------------------------------------------------------------------------------------
// |                           ErYang出品 属于小极品  O(∩_∩)O~~   共同学习    共同进步
// +----------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------
